#pragma once
#include<assert.h>
#include<vector>
namespace buluo
{

	template<class T>
	struct less
	{
		bool operator()(const T& x, const T& y)
		{
			return x < y;
		}
	};
	template<class T>
	struct greater
	{
		bool operator()(const T& x, const T& y)
		{
			return x > y;
		}
	};

	template<class T, class Container = vector<T>, class Compare = less<T>>
	class priority_queue
	{
	public:

		void adjustup(size_t child)
		{
			size_t parent = (child - 1) / 2;

			while (child > 0)
			{
				
				//if (_con[parent] < _con[child])
				if(Compare()(_con[parent], _con[child]))
				{
					swap(_con[parent], _con[child]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void adjustdown(size_t parent)
		{
			size_t child = parent * 2 + 1;
			while (child < _con.size())
			{
				//if (child + 1 < _con.size() && _con[child] < _con[child + 1])
				if (child + 1 < _con.size() && Compare()(_con[child], _con[child + 1]))
				{
					child++;
				}
				//if (_con[parent] < _con[child])
				if (Compare()(_con[parent], _con[child]))
				{
					swap(_con[parent], _con[child]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}
		void push(const T& s)
		{
			_con.push_back(s);
			adjustup(_con.size() - 1);

		}

		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			adjustdown(0);
		}

		const T& top()
		{
			return _con[0];
		}

		size_t size()
		{
			return _con.size();
		}
		bool empty()
		{
			return _con.size() == 0;
		}
	private:
		Container _con;

	};
	void test_priority_queue()
	{
		priority_queue<int,vector<int>,greater<int>> pq;
		pq.push(9);
		pq.push(5);
		pq.push(8);
		pq.push(6);
		pq.push(7);
		pq.push(2);
		pq.push(3);
		pq.push(1);
		pq.push(0);

		while (!pq.empty())
		{
			cout << pq.top() << " ";
			pq.pop();
		}
	}
}